﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame
{
    class UIItem : Entity
    {

        protected KeyboardState KeyboardInfo;
        protected MouseState MouseInfo;

        protected List<Keys> ExcludedKeys = new List<Keys>(new Keys[]
        {
            Keys.LeftShift,
            Keys.RightShift
        });

        protected KeyboardState LastKeyboard;

        public override void Update()
        {
            this.KeyboardInfo = Keyboard.GetState();
            this.MouseInfo = Mouse.GetState();
        }

        protected void PostDown()
        {
            this.LastKeyboard = this.KeyboardInfo;
        }

        protected bool IsKeyDown(Keys key)
        {
            return this.KeyboardInfo.IsKeyDown(key);
        }

        protected bool IsFirstPress(Keys key)
        {
            return this.KeyboardInfo.IsKeyDown(key) && !this.LastKeyboard.IsKeyDown(key);
        }

        protected string GetKeyboardChar()
        {
            string text = "";

            foreach (Keys item in this.KeyboardInfo.GetPressedKeys())
            {
                //Console.WriteLine(item.ToString() + " : stage 1");
                if (KeyboardHelper.IsCharactorKey(item))
                {
                    if (this.IsFirstPress(item) || this.ExcludedKeys.Contains(item))
                    {
                        //Console.WriteLine(item.ToString() + " : stage 2");
                        if (KeyboardInfo.IsKeyDown(Keys.LeftShift) || this.KeyboardInfo.IsKeyDown(Keys.RightShift))
                        {
                            //Console.WriteLine(item.ToString() + " : stage 3 shift");
                            text += KeyboardHelper.KeyToCharactor(item, true);
                        }
                        else
                        {
                            //Console.WriteLine(item.ToString() + " : stage 3 nonshift");
                            text += KeyboardHelper.KeyToCharactor(item, false);
                        }
                    }
                }
            }
            return text;
        }
    }
}
